const app = getApp()
const WXAPI = require('../../wxapi/main')
const Touches = require('./../../utils/Touches.js') 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mainType: ["需求市场", "已完成需求"],
    currentMainType: 0,
    statusType: ["全部", "洽谈中", "已完成"],
    currentType: 0,
    tabClass: ["", ""],

    searchSongList: [], //放置返回数据的数组  
    isFromSearch: true, // 用于判断searchSongList数组是不是空数组，默认true，空的数组
    searchPageNum: 1, // 设置加载的第几次，默认是第一次
    callbackcount: 5, //返回数据的个数
    searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
    searchLoadingComplete: false, //“没有数据”的变量，默认false，隐藏 

  },

  touchS: function (e) {  // touchstart
    let startX = Touches.getClientX(e)
    startX && this.setData({ startX })
  },
  touchM: function (e) {  // touchmove
    let searchSongList = Touches.touchM(e, this.data.searchSongList, this.data.startX)
    searchSongList && this.setData({ searchSongList })

  },
  touchE: function (e) {  // touchend
    const width = 200  // 定义操作列表宽度
    let searchSongList = Touches.touchE(e, this.data.searchSongList, this.data.startX, width)
    searchSongList && this.setData({ searchSongList })
  },
  mainTap: function (e) {
    console.log('e ', e)
    const mainType = e.currentTarget.dataset.index;
    console.log('main type ', mainType)
    this.setData({
      currentMainType: mainType,
      currentType: 0
    });
    this.setData({
      searchPageNum: 1, //第一次加载，设置1  
      searchSongList: [], //放置返回数据的数组,设为空  
      isFromSearch: true, //第一次加载，设置true  
      searchLoading: true, //把"上拉加载"的变量设为true，显示  
      searchLoadingComplete: false //把“没有数据”设为false，隐藏  
    })
    this.onShow();
  },

  statusTap: function (e) {
    const curType = e.currentTarget.dataset.index;
    this.data.currentType = curType
    this.setData({
      currentType: curType
    });
    this.setData({
      searchPageNum: 1, //第一次加载，设置1  
      searchSongList: [], //放置返回数据的数组,设为空  
      isFromSearch: true, //第一次加载，设置true  
      searchLoading: true, //把"上拉加载"的变量设为true，显示  
      searchLoadingComplete: false //把“没有数据”设为false，隐藏  
    })
    this.onShow();
  },
  orderReset: function (e) {      
    console.log('reset req id ', e.currentTarget.dataset.id)
    wx.showModal({
      title: '重置需求',
      content: '重置需求后，需求详情中的洽谈信息将被清空，需求将恢复初始状态。',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          let postData = {id: e.currentTarget.dataset.id}
          WXAPI.resetReq(postData)
          .then(res => {
            console.log("res => ", res)
            if (res.code === 0 ) {
              let ret = res.data
              console.log('ret ',ret)
              wx.showToast({
                title: '重置成功',
                icon: 'success'
              }) 
              that.onShow()            
            } else {
              wx.showToast({
                title: res.message,
                icon:'error'
              })
            }
          })
          // 删除 
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  
  orderCancel: function (e) {
    console.log('reset req id ', e.currentTarget.dataset.id)
    let that = this
    wx.showModal({
      title: '删除需求',
      content: '删除数据后将不可恢复',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          let postData = {id: e.currentTarget.dataset.id}
          WXAPI.deleteReq(postData)
          .then(res => {
            console.log("res => ", res)
            if (res.code === 0 ) {
              let ret = res.data
              console.log('ret ',ret)
              wx.showToast({
                title: '删除成功',
                icon: 'success'
              })  
              that.onShow()
            } else {
              wx.showToast({
                title: res.message,
                icon:'error'
              })
            }
          })
          // 删除 
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  gridchange: function (e) {
    this.setData({
      gridCol: e.detail.value
    });
  },
  gridswitch: function (e) {
    this.setData({
      gridBorder: e.detail.value
    });
  },
  menuBorder: function (e) {
    this.setData({
      menuBorder: e.detail.value
    });
  },
  menuArrow: function (e) {
    this.setData({
      menuArrow: e.detail.value
    });
  },
  menuCard: function (e) {
    this.setData({
      menuCard: e.detail.value
    });
  },
  switchSex: function (e) {
    this.setData({
      skin: e.detail.value
    });
  },

  giveUp: function (e) {      
    let that = this
    console.log('give up your task id ', e.currentTarget.dataset.id)
    wx.showModal({
      title: '放弃需求',
      content: '放弃需求后，需求详细中的洽谈信息将被清空，需求将恢复至需求市场。我接受的需求列表中将不再展示。',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          let postData = {taskId: e.currentTarget.dataset.id}
          WXAPI.giveupReq(postData)
          .then(res => {
            console.log("res => ", res)
            if (res.code === 0 ) {
              let ret = res.data
              console.log('ret ',ret)
              wx.showToast({
                title: '放弃成功',
                icon: 'success'
              })
              that.onShow()             
            } else {
              wx.showToast({
                title: res.message,
                icon:'error'
              })
            }
          })
          // 删除 
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //滚动到底部触发事件  
  searchScrollLower: function () {
    let that = this;
    if (that.data.searchLoading && !that.data.searchLoadingComplete) {
      that.setData({
        searchPageNum: that.data.searchPageNum + 1, //每次触发上拉事件，把searchPageNum+1  
        isFromSearch: false //触发到上拉事件，把isFromSearch设为为false  
      });
      that.onShow();
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true
    })
    wx.hideTabBarRedDot({
      index: 1,
    })
   
    // 禁止转发
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getOrderStatistics: function () {
    var that = this;
    WXAPI.orderStatistics(wx.getStorageSync('loginFlag')).then(function (res) {
      if (res.code == 0) {
        var tabClass = that.data.tabClass;
        if (res.data.count_id_no_pay > 0) {
          tabClass[0] = "red-dot"
        } else {
          tabClass[0] = ""
        }
        if (res.data.count_id_no_transfer > 0) {
          tabClass[1] = "red-dot"
        } else {
          tabClass[1] = ""
        }
        if (res.data.count_id_no_confirm > 0) {
          tabClass[2] = "red-dot"
        } else {
          tabClass[2] = ""
        }
        if (res.data.count_id_no_reputation > 0) {
          tabClass[3] = "red-dot"
        } else {
          tabClass[3] = ""
        }
        if (res.data.count_id_success > 0) {
          tabClass[4] = "red-dot"
        } else {
          tabClass[4] = ""
        }

        that.setData({
          tabClass: tabClass,
        });
      }
    })
  },

  toDetail: function (e) {
    console.log('明细 ', e)
    var that = this
    var id = e.currentTarget.dataset.id;
    console.log('ready enter into req detail page ', id)
    wx.navigateTo({
      url: "/pages/req-detail/index?reqId=" + id
    })
  },

  goChat: function (e) {
    console.log('聊天 ', e)
    var that = this
    var id = e.currentTarget.dataset.id;
    console.log('ready enter into chat page ', id)
    wx.navigateTo({
      url: "/pages/req/detail?reqId=" + id
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {   
    // 获取订单列表
    var that = this;
    var _partUserInfo = wx.getStorageSync('partUserInfo')
    if (!_partUserInfo) {
      app.goLoginPageTimeOut()
      return
    } else {
      that.setData({
        partUserInfo: _partUserInfo
      })
    }   
    var skey = wx.getStorageSync('loginFlag')
    console.log('_partUserInfo', _partUserInfo)
    console.log('skey ', skey)

    var postData = {
      skey: skey,
      status: that.data.currentType,
      searchPageNum: that.data.searchPageNum,
      callbackcount: that.data.callbackcount,
    };
    console.log('require ', postData)
    if ( that.data.currentMainType == 0) {
      WXAPI.loadMyReqList(postData).then(function (res) {
        console.log('我发布的刷新了', res)
        if (res.code == 0) {
          let retData = res.data
          if (retData.length > 0) {
            let searchList = [];
            //如果isFromSearch是true从data中取出数据，否则先从原来的数据继续添加  
            that.data.isFromSearch ?
              searchList = retData :
              searchList = that.data.searchSongList.concat(retData)
              console.log(searchList)
            if (retData.length <= that.data.callbackcount) {
              that.setData({
                searchSongList: searchList, //获取数据数组 
                searchLoadingComplete: true, //把“没有数据”设为true，显示  
                searchLoading: false //把"上拉加载"的变量设为false，隐藏  
              });
            } else {
              that.setData({
                searchSongList: searchList, //获取数据数组 
                searchLoading: true //把"上拉加载"的变量设为false，显示 
              });
            }
            //没有数据了，把“没有数据”显示，把“上拉加载”隐藏          
          } else {
            let searchList = [];
            that.data.isFromSearch ? searchList = [] :
              searchList = that.data.searchSongList
            that.setData({
              searchSongList: searchList, //获取数据数组 
              searchLoadingComplete: true, //把“没有数据”设为true，显示  
              searchLoading: false //把"上拉加载"的变量设为false，隐藏  
            })
          }
        } else {
          that.setData({
            searchSongList: null
          })
        }
      })
    } else {
      WXAPI.loadYourReqList(postData).then(function (res) {
        console.log('我接收的刷新了', res)
        if (res.code == 0) {
          let retData = res.data
          if (retData.length > 0) {
            let searchList = [];
            //如果isFromSearch是true从data中取出数据，否则先从原来的数据继续添加  
            that.data.isFromSearch ?
              searchList = retData :
              searchList = that.data.searchSongList.concat(retData)
            if (retData.length <= that.data.callbackcount) {
              that.setData({
                searchSongList: searchList, //获取数据数组 
                searchLoadingComplete: true, //把“没有数据”设为true，显示  
                searchLoading: false //把"上拉加载"的变量设为false，隐藏  
              });
            } else {
              that.setData({
                searchSongList: searchList, //获取数据数组 
                searchLoading: true //把"上拉加载"的变量设为false，显示 
              });
            }
            //没有数据了，把“没有数据”显示，把“上拉加载”隐藏          
          } else {
            let searchList = [];
            that.data.isFromSearch ? searchList = [] :
              searchList = that.data.searchSongList
            that.setData({
              searchSongList: searchList, //获取数据数组 
              searchLoadingComplete: true, //把“没有数据”设为true，显示  
              searchLoading: false //把"上拉加载"的变量设为false，隐藏  
            })
          }
        } else {
          that.setData({
            searchSongList: null
          })
        }
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('onPullDownRefresh...')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    console.log('onReachBottom ...')
    var that = this
    that.setData({
      searchPageNum: that.data.searchPageNum + 1,
      isFromSearch: false
    })
    that.onShow()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {    
  }
})